﻿using Dapper;
using Dapper.Contrib.Extensions;
using SmartForm.Common;
using SmartlForm.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartlForm.DAL
{
    public class GroupDao
    {
        public readonly static GroupDao Instance = new GroupDao();

        /// <summary>
        /// 获取统计表
        /// </summary>
        /// <param name="account_id"></param>
        /// <returns></returns>
        public AccountGroup Get(long account_id)
        {
            var group = DataBase.DB.QueryFirstOrDefault<AccountGroup>("SELECT * FROM account_group WHERE account_id=@account_id", new { account_id = account_id });
            if (group == null)
            {
                group = AddUseCount(account_id);
            }
            return group;
        }



        /// <summary>
        /// 更新使用次数
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public AccountGroup AddUseCount(long account_id)
        {
            AccountGroup model = new AccountGroup();
            model.account_id = account_id;
            model.send_num = 0;
            model.use_num = 0;
            DataBase.DB.Insert(model);
            return model;
        }


        /// <summary>
        /// 更新使用次数
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public void UpdateUseCount(long account_id)
        {
            DataBase.DB.Execute("Update account_group set use_num=use_num+1 WHERE account_id=@account_id", new { account_id = account_id });

        }

        /// <summary>
        /// 更新发送次数
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public void UpdateSendCount(long account_id)
        {
            DataBase.DB.Execute("Update account_group set send_num=send_num+1 WHERE account_id=@account_id", new { account_id = account_id });
        }

    }
}
